<template>
  <a-drawer
    :title="formInfo.id==undefined?'新增':'修改'"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    @close="handleCancel"
    :destroyOnClose="true"
    :visible="visible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
            <a-row>
              <a-col :span="12">
            <a-form-model-item label="项目编码" prop="itemCode" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.itemCode" placeholder="请输入项目编码"/>
            </a-form-model-item>
          </a-col>
                  <a-col :span="12">
            <a-form-model-item label="项目名称" prop="itemName" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.itemName" placeholder="请输入项目名称"/>
            </a-form-model-item>
          </a-col>
            </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="线上名称" prop="itemByname" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.itemByname" placeholder="请输入线上名称"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="检查分类" prop="itemType" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.itemType" placeholder="请选择检查分类">
                <a-select-option v-for="(item) in this.$sysDictOptions('checkType')":key="item.k" :value="parseInt(item.k)">
                  {{item.v}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="所属科室" prop="departCode" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.departCode" placeholder="请选择科室">
                <a-select-option v-for="(item) in this.departlist" :key="item.id" :value="item.id">
                  {{item.depart_name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目原价" prop="fullPrice" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.fullPrice" placeholder="请输入成本价格"/>
            </a-form-model-item>
          </a-col>
        </a-row>
                <a-row>
              <a-col :span="12">
            <a-form-model-item label="执行价格" prop="exePrice" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.exePrice" placeholder="请输入执行价格"/>
            </a-form-model-item>
          </a-col>
                  <a-col :span="12">
            <a-form-model-item label="项目分类" prop="itemType" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.itemType" placeholder="请选择项目分类">
                <a-select-option v-for="(item) in this.itemTypelist" :key="item.id" :value="item.id">
                  {{item.type_name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>
              <a-col :span="12">
            <a-form-model-item label="适用性别" prop="forsex" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.forsex" placeholder="请选择适用性别">
              <a-select-option v-for="(item) in this.$sysDictOptions('suitSex')":key="item.k" :value="parseInt(item.k)">
                {{item.v}}
              </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
                  <a-col :span="12">
            <a-form-model-item label="是否空腹" prop="isEmpty" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.isEmpty" placeholder="请选择是否空腹">
              <a-select-option v-for="(item) in this.$sysDictOptions('whetherFlag')" :key="item.k" :value="parseInt(item.k)">
                {{item.v}}
              </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>
              <a-col :span="12">
            <a-form-model-item label="未婚不适用" prop="virginDisabled" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.virginDisabled" placeholder="请选择是否未婚不适用">
              <a-select-option v-for="(item) in this.$sysDictOptions('whetherFlag')" :key="item.k" :value="parseInt(item.k)">
                {{item.v}}
              </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
                  <a-col :span="12">
            <a-form-model-item label="费用类项目" prop="isfee" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.isfee" placeholder="请选择是否费用类项目">
                <a-select-option v-for="(item) in this.$sysDictOptions('whetherFlag')" :key="item.k" :value="parseInt(item.k)">
                  {{item.v}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item label="是否启用" prop="isEnable"  :maxLength="50">
                      <a-select v-model="formInfo.isEnable" placeholder="请选择是否启用">
                        <a-select-option v-for="(item) in this.$sysDictOptions('whetherFlag')" :key="item.k" :value="parseInt(item.k)">
                          {{item.v}}
                        </a-select-option>
                      </a-select>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
            <a-form-model-item label="排序" prop="orderNum" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.orderNum" type="number" placeholder="请输入排序"/>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>

            </a-row>
        <a-row>

          <a-col :span="12">
            <a-form-model-item label="院区通用" prop="orderNum"  :maxLength="50">
              <a-checkbox v-model="isChecked" style="margin-top: 13px" @change="checkIs($event)"/>是否院区通用
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目组别" prop="groupId" :maxLength="50">
              <a-select v-model="formInfo.groupId" placeholder="请选择项目组别">
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option v-for="(item) in this.itemGrouplist" :key="item.id" :value="item.id">
                  {{item.group_name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
                <a-row>

                    <a-col :span="24">
                      <a-form-model-item  :labelCol="$global.labelTextAreaCol" :rules="$valid.required" :wrapperCol="$global.wrapperTextAreaCol"  label="检查目的">
                        <a-input v-model="formInfo.inspectPurpose" type="textarea" style="height: 100px;"/>
                      </a-form-model-item>
                    </a-col>

                </a-row>
            <a-row>
            <a-col :span="24">
                <a-form-model-item  :labelCol="$global.labelTextAreaCol"  :wrapperCol="$global.wrapperTextAreaCol"  label="注意事项">
                    <a-input v-model="formInfo.remark" type="textarea" style="height: 70px"/>
                </a-form-model-item>
            </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="confirmLoading">提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  import TagSelectOption from "@/components/TagSelect/TagSelectOption";
  import {getDepartlist, getItemGroupList, getItemTypeList} from "@/api/api";
  export default {
    name: 'HealthOrderItemEdit',
    components: {
      TagSelectOption
    },
    data () {
      return {
        visible: false,
        itemTypelist: [],
        departlist: [],
        itemGrouplist: [],
        confirmLoading: false,
        isChecked: false,
        formInfo: {
          itemCode: undefined,
          itemName: undefined,
          inspectPurpose: undefined,
          attention: undefined,
          hisno: undefined,
          pym: undefined,
          exePrice: undefined,
          costPrice: undefined,
          fullPrice: undefined,
          lowestPrice: undefined,
          forsex: undefined,
          isEmpty: undefined,
          virginDisabled: undefined,
          isfee: undefined,
          htype: undefined,
          orderNum: undefined,
          areaId: undefined,
          isEnable: undefined,
          createTime: undefined,
          departCode: undefined,
          grouId: undefined,
          remark: undefined,
          wx: ''
        },
        drawerWidth: 800,
        url: {
          saveInfo: '/system/healthOrderItem/save',
          getInfo: '/system/healthOrderItem/info'
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit()
      },
      edit (record) {
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
              this.formInfo = res.result.data
              if(this.formInfo.areaId==0){
                 this.isChecked = true;
              }else{
                this.isChecked = false;
              }
            })
          })
        }else{
          this.formInfo.forsex = 2
          this.formInfo.isEmpty = 1
          this.formInfo.virginDisabled = 0
          this.formInfo.isfee = 0
          this.formInfo.isEnable = 1
        }
        this.visible = true
        this.getdepartList()
        this.getItemTypeList()
        this.getitemGroupList()
      },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      checkIs(item){
          if(event.target.checked){
            this.$message.success('000')
           this.formInfo.areaId = '0'
          }else{
            this.formInfo.areaId = undefined
        }
      },
      // 关闭
      handleCancel () {
        this.close()
      },
      getdepartList(){
        getDepartlist({ }).then((res) => {
          const data = res.result.data
          this.departlist = data
        })
      },
      getItemTypeList(){
        getItemTypeList({ }).then((res) => {
          const data = res.result.data
          this.itemTypelist = data
        })
      },
      getitemGroupList(){
        getItemGroupList({ }).then((res) => {
          const data = res.result.data
          this.itemGrouplist = data
        })
      },
      close () {
        this.$refs.ruleForm.resetFields()
        this.$objToReset(this.formInfo)
        this.$emit('close')
        this.visible = false
      },
      editorOk (val) {
        this.formInfo.content = val
      }
    }
  }
</script>
